package com.tanhua.app.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.api.service.FriendApi;
import com.tanhua.api.service.UserApi;
import com.tanhua.api.service.UserInfoApi;
import com.tanhua.app.Interceptor.UserHolder;
import com.tanhua.app.exception.BusinessException;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.pojo.User;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessagesService {

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private FriendApi friendApi;

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    public UserInfoVo findByHxUser(String huanxinId) {
        // 根据环信id查询用户信息
        User user = userApi.findByHxUser(huanxinId);

        if (user == null) {
            throw new BusinessException(ErrorResult.HuanXinError());
        }

        // 根据用户id查询用户详情信息
        UserInfo userInfo = userInfoApi.findById(user.getId());

        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, vo);
        if (userInfo.getAge() != null) {
            vo.setAge(userInfo.getAge().toString());
        }

        return vo;
    }

    // 添加好友
    public void saveFriend(Long userId) {

        // 获取当前用户id
        Long toUserId = UserHolder.getUserId();

        // 在环信上添加好友
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + userId,
                Constants.HX_USER_PREFIX + toUserId);

        if (!aBoolean) {
            throw new BusinessException(ErrorResult.HuanXinAddError());
        }

        // 添加好友到数据库
        friendApi.saveFriend(userId, toUserId);

    }

    // 联系人列表数据
    public PageResult<ContactVo> findUserInfoByName(Integer page, Integer pagesize, String keyword) {

        // 根据用户id,查询好友列表
        List<Long> userIds = friendApi.findByToUserId(UserHolder.getUserId());

        // 判断是否有数据
        if (CollUtil.isEmpty(userIds)) {
            return new PageResult<>();
        }

        // 根据用户id列表和用户名称分页查询，用户详情数据
        Page<UserInfo> pages =  userInfoApi.findByIdsLikeName(userIds,keyword, page, pagesize);

        // 分装数据
        List<ContactVo> list = new ArrayList<>();

        List<UserInfo> userInfos = pages.getRecords();
        for (UserInfo userInfo : userInfos) {
            ContactVo vo = ContactVo.init(userInfo);

            list.add(vo);
        }

        return new PageResult<>(page, pagesize, pages.getTotal(), list);

    }

    // 删除联系人
    public void deleteFriend(Long friendId) {

        // 获取当前用户id
        Long userId = UserHolder.getUserId();

        // 删除环信上的好友
        Boolean aBoolean = huanXinTemplate.deleteContact(Constants.HX_USER_PREFIX + userId,
                Constants.HX_USER_PREFIX + friendId);

        if (!aBoolean) {
            throw new BusinessException(ErrorResult.HuanXinDropError());
        }

        // 删除联系人
        friendApi.deleteFriend(userId, friendId);

    }
}
